package ca.ets.osgi.uiservice_model;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 * Set the data of the list and the users
 * 
 * @author HP_Administrator
 * 
 */
public class ModeleMainService extends Observable {

	// Attributs
	private ListRoomUsers lu = ListRoomUsers.getInstance();
	private RoomUser user;
	private ListRoomsInfo li = ListRoomsInfo.getInstance();
	private RoomInfo roomi;

	/**
	 * Constructeur
	 */
	public ModeleMainService(Observer o) {
		this.addObserver(o);
	}

	/**
	 * Return the list of RoomUsers
	 * 
	 * @return
	 */
	public ListRoomUsers getListRoomUsers() {
		return this.lu;
	}

	/**
	 * Return the list of RoomsInfo
	 * 
	 * @return
	 */
	public ListRoomsInfo getListRoomsInfo() {
		return this.li;
	}

	/**
	 * @return the user
	 */
	public RoomUser getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(RoomUser user) {
		this.user = user;
	}

	/**
	 * Return the RoomUser from the list
	 * 
	 * @param ID
	 * @return
	 */
	public RoomUser getUser(String ID) {
		return this.lu.getRoomUser(ID);
	}

	/**
	 * @return the roomID
	 */
	public String getRoomID() {
		return this.user.getRoomID();
	}

	/**
	 * @param roomID
	 *            the roomID to set
	 */
	public void setRoomID(String roomID) {
		this.user.setRoomID(roomID);
	}

	/**
	 * @return the date
	 */
	public String getDate() {
		return this.user.getDate();
	}

	/**
	 * @param date
	 *            the date to set
	 */
	public void setDate(String date) {
		this.user.setDate(date);
	}

	/**
	 * @return the hourStart
	 */
	public String getHourStart() {
		return this.user.getHourStart();
	}

	/**
	 * @param hourStart
	 *            the hourStart to set
	 */
	public void setHourStart(String hourStart) {
		this.user.setHourStart(hourStart);
	}

	/**
	 * @return the hourEnd
	 */
	public String getHourEnd() {
		return this.user.getHourEnd();
	}

	/**
	 * @param hourEnd
	 *            the hourEnd to set
	 */
	public void setHourEnd(String hourEnd) {
		this.user.setHourEnd(hourEnd);
	}

	/**
	 * @return the isRoomYours
	 */
	public boolean isRoomYours() {
		return this.user.isRoomYours();
	}

	/**
	 * @param isRoomYours the isRoomYours to set
	 */
	public void setRoomYours(boolean isRoomYours) {
		this.user.setRoomYours(isRoomYours);
	}

	/**
	 * @return the reserver
	 */
	public String getReserver() {
		return this.user.getReserver();
	}

	/**
	 * @param reserver
	 *            the reserver to set
	 */
	public void setReserver(String reserver) {
		this.user.setReserver(reserver);
	}

	/**
	 * @return the surface
	 */
	public String getSurface() {
		return user.getSurface();
	}

	/**
	 * @param surface
	 *            the surface to set
	 */
	public void setSurface(String surface) {
		this.user.setSurface(surface);
	}

	/**
	 * @return the isProjectorAvailable
	 */
	public boolean isProjectorAvailable() {
		return this.user.isProjectorAvailable();
	}

	/**
	 * @param isProjectorAvailable
	 *            the isProjectorAvailable to set
	 */
	public void setProjectorAvailable(boolean isProjectorAvailable) {
		this.user.setProjectorAvailable(isProjectorAvailable);
	}

	/**
	 * Return the RoomUserInformation
	 * 
	 * @return
	 */
	public String userToString() {
		return "This room " + this.user.getRoomID() + ", " + "reserver "
				+ this.user.getReserver() + ", on " + this.user.getDate()
				+ " From " + this.user.getHourStart() + " Until "
				+ this.user.getHourEnd();
	}

	/**
	 * 
	 * @param surface
	 * @param projector
	 * @param reserver
	 * @param date
	 * @param hourStart
	 * @param hourEnd
	 */
	public void createRoomUser(String surface, boolean isProjectorAvailable,
			String date, String reserver, String hourStart, String hourEnd) {
		RoomUser ru = new RoomUser();
		ru.setDate(date);
		ru.setSurface(surface);
		ru.setProjectorAvailable(isProjectorAvailable);
		ru.setReserver(reserver);
		ru.setHourStart(hourStart);
		ru.setHourEnd(hourEnd);
		this.user = ru;
		lu.addRoomUserToTable(reserver, ru);
	}

	/**
	 * Return a room user
	 * 
	 * @param ID
	 * @return
	 */
	public RoomUser getRoomUser(String ID) {
		return this.lu.getRoomUser(ID);
	}

	/**
	 * 
	 * @param ID
	 * @return
	 */
	public boolean isRoomUserInTable(String ID) {
		return this.lu.isRoomUserInTable(ID);
	}

	/**
	 * Set the list of valids rooms with the user criterion
	 * 
	 * @param obj
	 */
	@SuppressWarnings("unchecked")
	public void setListRoomsInfo(ArrayList<Object> obj) {
		ArrayList<String> listRoomsID = new ArrayList<String>();
		listRoomsID.add("rooms");
		for (Object first : (ArrayList<Object>) obj) {
			RoomInfo room = new RoomInfo();
			((ArrayList<Object>) first).get(0);
			room.setRoomID((String) ((ArrayList<Object>) first).get(0));
			room.setDate((String) ((ArrayList<Object>) first).get(1));
			room.setHourStart((String) ((ArrayList<Object>) first).get(2));
			room.setHourEnd((String) ((ArrayList<Object>) first).get(3));
			li.addRoomInfoToTable(room.getRoomID(), room);
			listRoomsID.add(room.getRoomID());

		}
		
		System.err.println("Updating List of Rooms Available");
		System.out.println("");
		setChanged();
		notifyObservers(listRoomsID);
	}
	
	@SuppressWarnings("unchecked")
	public void setUserInfo(ArrayList<Object> alist){
		ArrayList<String> listUserInfo = new ArrayList<String>();
		listUserInfo.add("user");
		if(alist !=null){
			if(!alist.isEmpty()){
				this.user.setRoomID((String) ((ArrayList<Object>) alist).get(0));
				this.user.setReserver((String) ((ArrayList<Object>) alist).get(1));
				this.user.setDate((String) ((ArrayList<Object>) alist).get(2));
				this.user.setHourStart((String) ((ArrayList<Object>) alist).get(3));
				this.user.setHourEnd((String) ((ArrayList<Object>) alist).get(4));
				this.user.setRoomYours((Boolean) ((ArrayList<Object>) alist).get(5));
				listUserInfo.add(this.userRequest());
				listUserInfo.add("true");
			}
		}
		
		else{
			listUserInfo.add(this.userRequest());
			listUserInfo.add("false");
		}
		
		System.err.println("Updating user info");
		System.out.println("");
		setChanged();
		notifyObservers(listUserInfo);
	}
	
	/**
	 * 
	 * @return
	 */
	public String userToString2(){
		return "[User: " + this.user.getReserver() +  ", Surface: "  + this.user.getSurface() + ", Projector: " +this.user.isProjectorAvailable() + ", Date: " + this.user.getDate() + ", From: " + this.user.getHourStart() + ", To: " + this.user.getHourEnd() + "]"; 
	}
	
	/**
	 * 
	 * @return
	 */
	public String userRequest(){
		return "[User: " + this.user.getReserver() +  ", Surface: "  + this.user.getSurface() + ", Projector: " +this.user.isProjectorAvailable() + ", Date: " + this.user.getDate() + ", From: " + this.user.getHourStart() + ", To: " + this.user.getHourEnd() + ",is it your's: " + this.user.isRoomYours() + "]"; 

	}

	/**
	 * 
	 * @param ID
	 */
	public void setThisRoomInfo(String ID) {

	}

}
